using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace DeskNotes
{
	/// <summary>
	/// Summary description for PrintPreviewForm.
	/// </summary>
	public class PrintPreviewForm : System.Windows.Forms.Form
	{
		const double ZOOM_STEP = 0.1;
		const double MINIMUM = 0.05;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.ToolBarButton btnPrint;
		private System.Windows.Forms.ToolBarButton btnSettings;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.PrintPreviewControl printPreview;
		private System.Windows.Forms.ToolBarButton toolBarButton2;
		private System.Windows.Forms.ToolBarButton btn1Page;
		private System.Windows.Forms.ToolBarButton btn2Page;
		private System.Windows.Forms.ToolBarButton btn4Page;
		private System.Windows.Forms.ToolBarButton btnZoomUp;
		private System.Windows.Forms.ToolBarButton btnZoomDown;
		private System.Windows.Forms.ToolBarButton toolBarButton3;
		private System.Windows.Forms.ImageList imageList;
		private System.Windows.Forms.ToolBar toolBar;
		private System.ComponentModel.IContainer components;

		private IPrintableNote[] notes;
		private System.Windows.Forms.PageSetupDialog pageSetupDialog;
		private System.Windows.Forms.NumericUpDown pageUpDown;

		private readonly DeskNotes deskNotes;

				
		public PrintPreviewForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		internal PrintPreviewForm(DeskNotes deskNotes, IPrintableNote note)
		{
			InitializeComponent();
			this.deskNotes = deskNotes;
			this.notes = new IPrintableNote[1];
			this.notes[0] = note;
		}

		internal PrintPreviewForm(DeskNotes deskNotes, IPrintableNote[] notes)
		{
			InitializeComponent();
			this.deskNotes = deskNotes;
			this.notes = notes;
		}

		internal new void Show()
		{
			NotePrintDocument doc = new NotePrintDocument(deskNotes.PageSettings, notes);
			printPreview.Document = doc;
			int width = SystemInformation.WorkingArea.Size.Width - 200;
			int height = SystemInformation.WorkingArea.Size.Height - 200;
			this.SetDesktopBounds( 100, 100, width, height);
			UpdateZoomInfo();			
			btn2Page.Pushed = false;
			btn4Page.Pushed = false;
			base.Show();
			WinImports.SetForegroundWindow(this.Handle);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PrintPreviewForm));
            this.toolBar = new System.Windows.Forms.ToolBar();
            this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
            this.btnPrint = new System.Windows.Forms.ToolBarButton();
            this.btnSettings = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
            this.btnZoomUp = new System.Windows.Forms.ToolBarButton();
            this.btnZoomDown = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
            this.btn1Page = new System.Windows.Forms.ToolBarButton();
            this.btn2Page = new System.Windows.Forms.ToolBarButton();
            this.btn4Page = new System.Windows.Forms.ToolBarButton();
            this.imageList = new System.Windows.Forms.ImageList(this.components);
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.pageUpDown = new System.Windows.Forms.NumericUpDown();
            this.label1 = new System.Windows.Forms.Label();
            this.printPreview = new System.Windows.Forms.PrintPreviewControl();
            this.pageSetupDialog = new System.Windows.Forms.PageSetupDialog();
            ((System.ComponentModel.ISupportInitialize)(this.pageUpDown)).BeginInit();
            this.SuspendLayout();
            // 
            // toolBar
            // 
            resources.ApplyResources(this.toolBar, "toolBar");
            this.toolBar.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
            this.toolBarButton3,
            this.btnPrint,
            this.btnSettings,
            this.toolBarButton1,
            this.btnZoomUp,
            this.btnZoomDown,
            this.toolBarButton2,
            this.btn1Page,
            this.btn2Page,
            this.btn4Page});
            this.toolBar.ImageList = this.imageList;
            this.toolBar.Name = "toolBar";
            this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
            this.toolBar.ButtonDropDown += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
            // 
            // toolBarButton3
            // 
            this.toolBarButton3.Name = "toolBarButton3";
            this.toolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // btnPrint
            // 
            resources.ApplyResources(this.btnPrint, "btnPrint");
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Tag = "Print";
            // 
            // btnSettings
            // 
            resources.ApplyResources(this.btnSettings, "btnSettings");
            this.btnSettings.Name = "btnSettings";
            this.btnSettings.Tag = "PrinterSettings";
            // 
            // toolBarButton1
            // 
            this.toolBarButton1.Name = "toolBarButton1";
            this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // btnZoomUp
            // 
            resources.ApplyResources(this.btnZoomUp, "btnZoomUp");
            this.btnZoomUp.Name = "btnZoomUp";
            this.btnZoomUp.Tag = "ZoomUp";
            // 
            // btnZoomDown
            // 
            resources.ApplyResources(this.btnZoomDown, "btnZoomDown");
            this.btnZoomDown.Name = "btnZoomDown";
            this.btnZoomDown.Tag = "ZoomDown";
            // 
            // toolBarButton2
            // 
            this.toolBarButton2.Name = "toolBarButton2";
            this.toolBarButton2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // btn1Page
            // 
            resources.ApplyResources(this.btn1Page, "btn1Page");
            this.btn1Page.Name = "btn1Page";
            this.btn1Page.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
            this.btn1Page.Tag = "OnePage";
            // 
            // btn2Page
            // 
            resources.ApplyResources(this.btn2Page, "btn2Page");
            this.btn2Page.Name = "btn2Page";
            this.btn2Page.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
            this.btn2Page.Tag = "TwoPages";
            // 
            // btn4Page
            // 
            resources.ApplyResources(this.btn4Page, "btn4Page");
            this.btn4Page.Name = "btn4Page";
            this.btn4Page.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
            this.btn4Page.Tag = "FourPages";
            // 
            // imageList
            // 
            this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
            this.imageList.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList.Images.SetKeyName(0, "");
            this.imageList.Images.SetKeyName(1, "");
            this.imageList.Images.SetKeyName(2, "");
            this.imageList.Images.SetKeyName(3, "");
            this.imageList.Images.SetKeyName(4, "");
            this.imageList.Images.SetKeyName(5, "");
            this.imageList.Images.SetKeyName(6, "");
            // 
            // pageUpDown
            // 
            resources.ApplyResources(this.pageUpDown, "pageUpDown");
            this.pageUpDown.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.pageUpDown.Name = "pageUpDown";
            this.toolTip1.SetToolTip(this.pageUpDown, resources.GetString("pageUpDown.ToolTip"));
            this.pageUpDown.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.pageUpDown.ValueChanged += new System.EventHandler(this.pageUpDown_Click);
            this.pageUpDown.Click += new System.EventHandler(this.pageUpDown_Click);
            // 
            // label1
            // 
            resources.ApplyResources(this.label1, "label1");
            this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label1.Name = "label1";
            // 
            // printPreview
            // 
            resources.ApplyResources(this.printPreview, "printPreview");
            this.printPreview.AutoZoom = false;
            this.printPreview.Name = "printPreview";
            this.printPreview.Zoom = 0.7;
            // 
            // PrintPreviewForm
            // 
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this.printPreview);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.pageUpDown);
            this.Controls.Add(this.toolBar);
            this.Name = "PrintPreviewForm";
            ((System.ComponentModel.ISupportInitialize)(this.pageUpDown)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion


		private void PrintDocument()
		{
			try {
				deskNotes.PrintNotes(notes, null);
			} 
			catch (System.Drawing.Printing.InvalidPrinterException ex)
			{
				MessageBox.Show(this, string.Format(MainResource.GetString("PRINTER_NOT_AVAILABLE"), ex.Message), 
					MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void PrinterSettings()
		{
			try 
			{
				pageSetupDialog.PageSettings = deskNotes.DisplayedPageSettings;
			
				if (DialogResult.OK == pageSetupDialog.ShowDialog(this))
				{
					deskNotes.PageSettings = pageSetupDialog.PageSettings;
					printPreview.Document = new NotePrintDocument(pageSetupDialog.PageSettings, notes);
				}
			} 
			catch (InvalidPrinterException)
			{
				MessageBox.Show(this, MainResource.GetString("PRINTER_NOT_AVAILABLE"), MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}


		/// <summary>
		/// Toolbar buttons handler. Uses Tag ToolbarButton 
		/// property to distinguish among buttons.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			string tag = (string)e.Button.Tag;
			switch(tag)
			{
				case "Print":
					PrintDocument();
					break;
				case "PrinterSettings":
					PrinterSettings();
					break;
				case "ZoomUp":
					printPreview.Zoom += ZOOM_STEP;
					UpdateZoomInfo();
					break;
				case "ZoomDown":
					if (MINIMUM < (printPreview.Zoom - ZOOM_STEP))
					{
						printPreview.Zoom -= ZOOM_STEP;
						UpdateZoomInfo();
					}
					break;
				case "OnePage":
					printPreview.Columns = 1;
					printPreview.Rows = 1;
					btn2Page.Pushed = false;
					btn4Page.Pushed = false;
					break;
				case "TwoPages":
					printPreview.Columns = 2;
					printPreview.Rows = 1;
					btn1Page.Pushed = false;
					btn4Page.Pushed = false;
					break;
				case "FourPages":
					printPreview.Columns = 2;
					printPreview.Rows = 2;
					btn2Page.Pushed = false;
					btn1Page.Pushed = false;
					break;
			}
		}
		private void UpdateZoomInfo()
		{
			btnZoomUp.ToolTipText = string.Format(MainResource.GetString("TXT_ZOOM"), printPreview.Zoom * 100);
			btnZoomDown.ToolTipText = string.Format(MainResource.GetString("TXT_ZOOM_DOWN"), printPreview.Zoom * 100);
		}

		private void pageUpDown_Click(object sender, System.EventArgs e)
		{
			int page = (int)pageUpDown.Value;
			printPreview.StartPage = page-1;
			if (page-1 !=  printPreview.StartPage)
			{
				pageUpDown.Value =  printPreview.StartPage+1;
			}
		}
	}
}
